package com.huiminpay.merchant.controller;

import com.huiminpay.common.cache.domain.BusinessCast;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.util.PhoneUtil;
import com.huiminpay.common.cache.util.SecurityUtil;
import com.huiminpay.common.cache.util.StringUtil;
import com.huiminpay.merchant.service.SmsService;
import com.huiminpay.merchant.vo.MerchantRegisterVo;
import com.yh.huiminpay.merchant.api.IMerchantService;
import com.yh.huiminpay.merchant.dto.MerchantDTO;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@Api("商务开发接口，短信验证，资源认证，商户注册")
public class MerchantController {
    @Reference
    IMerchantService iMerchantService;
    @Autowired
    SmsService smsService;



    /**
     * 测试
     * @param id
     * @return
     */
    @GetMapping("/find/{id}")
    public MerchantDTO queryMerchantById(@PathVariable("id") Long id) {
        return iMerchantService.queryMerchantById(id);
    }

    /**
     * 用户注册注册验证码服务
     * @param phone
     * @return
     */
    @GetMapping("/sms")
    @ApiImplicitParam(name = "phone",dataType = "String",required = true, paramType = "query")
    public String getSMSCode(@RequestParam("phone") String phone) {
      String ns=smsService.sendSms(phone);
      System.out.print(ns);
      return ns;
    }
    @ApiOperation("商户注册")
    @ApiImplicitParam(name = "merchantRegisterVO",value = "商户注册信息",dataType = "MerchantRegisterVO",required = true,paramType = "body")
    @PostMapping("/merchants/register")
    public MerchantRegisterVo registerMerchant(@RequestBody MerchantRegisterVo merchantRegisterVO){
        //判断是否为空
        if(merchantRegisterVO == null){
            BusinessCast.cast(CommonErrorCode.E_100101);
        }
        //判断账号跟密码是否为空
        if(StringUtils.isEmpty(merchantRegisterVO.getUsername())|| StringUtil.isEmpty(merchantRegisterVO.getPassword())){
            BusinessCast.cast(CommonErrorCode.E_200226);
        }
        MerchantRegisterVo registerVO = smsService.regiter(merchantRegisterVO);

        return registerVO;
    }
    /**
     * 上传证件照
     */
    @ApiOperation("资质申请,上传证件")
    @PostMapping("/upload")
    @ApiParam(name = "file",value = "文件信息")
    public  StringUtil upload(MultipartFile file){
        try {
            smsService.upload(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  null;
    }
     @ApiOperation("资质申请")
     @PostMapping("/my/merchants")
     @ApiImplicitParam(name = "merchantDTO", value = "资质申请", required = true, dataType = "MerchantDTO", paramType = "body")
          public  MerchantDTO applyMerchantt(@RequestBody MerchantDTO merchantDTO){
          Long merchantId = SecurityUtil.getMerchantId();
          MerchantDTO merchantDTO1 =iMerchantService.applyMerchant(merchantId,merchantDTO);
          return merchantDTO1;
    }
}

